/*
 * ExibirGraficoServlet.java
 *
 * Created on 29 de Janeiro de 2007, 14:01
 */

package cefetpb.nav.cartesius.servlets;

import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import javax.imageio.ImageIO;
import cefetpb.nav.cartesius.backingbeans.GlobalSessionBean;

/**
 *
 * @author RosbergLinhares
 * @version
 */
public class ExibirGraficoServlet extends HttpServlet {
    private static final short LARGURA_PLANO = 500;
    private static final short ALTURA_PLANO = 400;
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        BufferedImage img = new BufferedImage(LARGURA_PLANO, ALTURA_PLANO, BufferedImage.TYPE_INT_RGB);
        
        Graphics2D g = img.createGraphics();
        GlobalSessionBean.getInstance().getPlanoCartesiano().desenhar(g);
        g.dispose();
        
        response.setContentType("image/jpeg");
        OutputStream responseOutputStream = response.getOutputStream();
        
        try {
            ImageIO.write(img, "jpg", responseOutputStream);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        responseOutputStream.flush();
        responseOutputStream.close();
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}